package com.hbvtc.xxxy.nikeSale.frames;

import java.awt.Color;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.table.JTableHeader;
import com.hbvtc.xxxy.nikeSale.daos.Vipdao;
import com.hbvtc.xxxy.nikeSale.models.GoodsShop;
import com.hbvtc.xxxy.nikeSale.models.Vips;
import com.hbvtc.xxxy.nikeSale.utils.DbManager;

/**
 * 结算页面
 * 
 * @author XR
 * 
 */
public class OverNike extends JFrame implements ItemListener, ActionListener {
	// 设置时间的
	private static final long serialLVersionUID = 4306803332677233920L;

	JLabel jLabel1, jLabel2, jLabel3, jLabel4, jLabel5, jLabel6, jLabel7,
			jLabel8, jLabel9, jLabel10, jLabela, jLabelb, jLabelc, jLabeld,
			jLabele, jLabelf, jLabelg, jLabelh;
	JTextField jt1, jt2, jt3, jt4, jta, jtb, jtc;
	JButton jb1, jb2, jb3, jb4, jb5, jButton = new JButton();
	JPanel jPanel, jPanel2, jPanel3;
	JTable jTable1, jTable2, jTable3;
	List list, list1;
	JRadioButton jrb1, jrb2;
	ButtonGroup buttonGroup;
	JTableHeader jHeader;
	JPasswordField jps;
	Connection con;
	PreparedStatement pre, pre1, pre2;
	ResultSet res;
	String sql, string, sql2, ddd, sql3;
	int m = 0, shuliang = 0;
	GoodsShop goodsShop = new GoodsShop();
	double sum = 0, zonger = 0, xxx, o = 0, str, qqq, xzy, www, hhh;
	Object obj[][] = new Object[50][5];
	String[] str1 = { "商品编号", "商品名称", "商品数量", "商品单价", "商品总价" };
	Vipdao vipdao = new Vipdao();
	Vips vips = new Vips();
	Vips vips2 = new Vips();

	public OverNike() {
		this.setTitle("收银系统界面");
		this.setSize(1500, 900);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setResizable(false);
		// 设置标题栏的图标
		ImageIcon imageIcon = new ImageIcon(getClass().getResource("01.png"));
		this.setIconImage(imageIcon.getImage());

		this.setLayout(null);
		jPanel = new JPanel(null);
		setContentPane(jPanel);
		list = new List();
		con = DbManager.getconn();
		sql = "select GoodsID from goods";
		try {
			pre = con.prepareStatement(sql);
			res = pre.executeQuery();
			while (res.next()) {
				list.add(String.valueOf(res.getInt("GoodsID")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jLabel1 = new JLabel("订购商品编号:");
		jLabel3 = new JLabel("会员情况:");
		jLabel3.setFont(new Font(null, 0, 24));
		jLabel2 = new JLabel("订购商品数量:");
		jLabel4 = new JLabel();
		jLabel5 = new JLabel();
		jLabel6 = new JLabel();
		jLabel7 = new JLabel();
		jLabel8 = new JLabel();
		jLabel9 = new JLabel();
		jLabel10 = new JLabel("支付方式:");
		jPanel.add(jLabel10);

		list1 = new List();
		jPanel.add(list1);
		list1.add("pay cash", 0);
		list1.add("pay balance", 1);
		list1.add("Alipay payment", 2);
		list1.add("pay WeChat", 3);
		list1.add("pay bank card debit card", 4);
		list1.setBounds(520, 680, 260, 30);
		list1.setFont(new Font(null, 0, 21));

		jLabel8.setText("商品总数：" + shuliang);
		jLabel9.setText("商品总额:￥" + zonger + "元");
		jLabel9.setFont(new Font("黑体", 0, 46));
		jLabel9.setForeground(Color.red);
		jLabel8.setFont(new Font("黑体", 0, 28));
		jLabel10.setFont(new Font("黑体", 0, 28));
		jLabel1.setFont(new Font("黑体", 0, 20));
		jLabel2.setFont(new Font("黑体", 0, 20));

		// 设置时间变量
		JLabel time = new JLabel();
		time.setForeground(Color.red);
		time.setBounds(1250, 0, 200, 20);
		time.setFont(new Font("微软雅黑", Font.BOLD, 18));
		jPanel.add(time);
		setTimer(time);

		jPanel.add(jLabel8);
		jPanel.add(jLabel9);
		jLabel8.setBounds(0, 680, 200, 40);
		jLabel9.setBounds(900, 700, 550, 45);
		jLabel10.setBounds(350, 680, 250, 30);

		jt1 = new JTextField();
		jt2 = new JTextField();
		jb1 = new JButton("");
		jb2 = new JButton("添加");
		jb2.setFont(new Font(null, 0, 24));
		jb3 = new JButton("清空");
		jb3.setFont(new Font(null, 0, 24));
		jPanel.add(jb3);
		jb3.setBounds(1250, 570, 150, 40);
		jrb1 = new JRadioButton("是");
		jrb1.setFont(new Font(null, 0, 24));
		jrb2 = new JRadioButton("否");
		jrb2.setFont(new Font(null, 0, 24));
		buttonGroup = new ButtonGroup();
		buttonGroup.add(jrb1);
		buttonGroup.add(jrb2);

		jPanel.add(jLabel1);
		jPanel.add(jLabel2);
		jPanel.add(jLabel3);
		jPanel.add(jt1);
		jPanel.add(jt2);
		jPanel.add(jb1);
		jPanel.add(jb2);
		jPanel.add(list);
		jPanel.add(jrb1);
		jPanel.add(jrb2);

		jLabel1.setBounds(0, 0, 150, 30);
		list.setBounds(138, 6, 100, 22);
		jLabel2.setBounds(0, 45, 150, 30);
		jt1.setBounds(138, 45, 170, 28);
		jb2.setBounds(1250, 30, 200, 40);
		jrb1.setBounds(125, 580, 50, 30);
		jrb2.setBounds(188, 580, 50, 30);
		jLabel3.setBounds(0, 580, 150, 30);
		// Object[][] object=new Object[17][5];
		for (int n = 0; n < 5; n++) {
			obj[0][n] = null;
		}
		jTable1 = new JTable(obj, str1);
		JScrollPane jsp = new JScrollPane(jTable1);
		// jPanel.add(jTable1.getTableHeader()).setBounds(0, 95, 1200, 25);
		jPanel.add(jsp);
		jsp.setBounds(0, 105, 1500, 450);
		list.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				string = list.getSelectedItem();
				sql = "select GoodsName,SalePrice,StockNum from goods where GoodsID=?";
				con = DbManager.getconn();
				try {
					pre = con.prepareStatement(sql);
					pre.setInt(1, Integer.parseInt(string));
					res = pre.executeQuery();
					while (res.next()) {
						goodsShop.setTypeID(res.getString("GoodsName"));
						goodsShop.setSalePrice(res.getDouble("SalePrice"));
						goodsShop.setStockNum(res.getInt("StockNum"));
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				jLabel4.setText("商品名称:  " + goodsShop.getTypeID() + "  商品单价  "
						+ goodsShop.getSalePrice() + "  商品数量  "
						+ goodsShop.getStockNum());
				jPanel.add(jLabel4);
				jLabel4.setBounds(275, 0, 400, 30);
			}
		});
		jrb1.addActionListener(this);
		jrb2.addActionListener(this);
		jb1.addActionListener(this);
		jb2.addActionListener(this);
		jb3.addActionListener(this);

		list1.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				String w = list1.getSelectedItem();
				if (w == "pay cash") {
					new OverNike().cash(str);
				}
				if (w == "pay balance") {
					new OverNike().Sur(str, xzy, ddd, www, m, obj);
				}
				if (w == "Alipay payment") {
					JOptionPane.showMessageDialog(OverNike.this,
							"还未开发此功能请用现金或会员支付");
				}
				if (w == "pay WeChat") {
					JOptionPane.showMessageDialog(OverNike.this,
							"还未开发此功能请用现金或会员支付");
				}
				if (w == "pay bank card debit card") {
					JOptionPane.showMessageDialog(OverNike.this,
							"还未开发此功能请用现金或会员支付");
				}
			}
		});
	}

	public static void main(String[] args) {
		new OverNike();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		if (jrb1.isSelected()) {
			jLabel5.setText("请输入会员电话：");
			jPanel.add(jLabel5);
			jLabel5.setBounds(275, 580, 130, 30);
			jt2.setBounds(450, 580, 100, 30);
			jb1.setText("验证会员");
			jPanel.add(jb1);
			jb1.setBounds(588, 580, 100, 30);
			str = zonger;
			if (e.getSource() == jb1) {
				try {
					if (vipdao.sss(jt2.getText()) == true) {
						vips = vipdao.select(jt2.getText());
						jLabel6.setText("会员余额:"
								+ new BigDecimal(vips.getBalance()).setScale(2,
										BigDecimal.ROUND_HALF_UP));
						xzy = vips.getBalance();
						www = vips.getIntegration();
						ddd = jt2.getText();
						jLabel7.setText("会员积分:"
								+ new BigDecimal(vips.getIntegration())
										.setScale(2, BigDecimal.ROUND_HALF_UP));
						jPanel.add(jLabel6);
						jPanel.add(jLabel7);
						jLabel6.setBounds(725, 580, 150, 30);
						jLabel7.setBounds(913, 580, 200, 30);
						JOptionPane.showMessageDialog(OverNike.this, "有此会员信息");
						zonger = zonger * 0.85;
						str = zonger;
						jLabel9.setText("商品总额: ￥"
								+ new BigDecimal(zonger).setScale(2,
										BigDecimal.ROUND_HALF_UP) + "元");
						// 会员85折加保留两位小数;
					} else {
						JOptionPane.showMessageDialog(OverNike.this, "无此会员信息");
					}
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		if (jrb2.isSelected()) {
			jLabel5.setText("");
			jPanel.add(jLabel5);
			jLabel5.setBounds(275, 90, 0, 0);
			jt2.setBounds(450, 90, 0, 0);
			jb1.setText("");
			jPanel.add(jb1);
			jb1.setBounds(598, 90, 0, 0);
			jLabel6.setBounds(850, 30, 0, 0);
			jLabel7.setBounds(850, 90, 0, 0);
			try {
				if (vipdao.sss(jt2.getText()) == true)
					zonger = zonger / 0.85;
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			str = zonger;
			jLabel9.setText("商品总额：  ￥"
					+ new BigDecimal(zonger).setScale(2,
							BigDecimal.ROUND_HALF_UP) + "元");
		}
		if (e.getSource() == jb2) {
			if (jt1.getText().toString() == null) {
				JOptionPane.showMessageDialog(OverNike.this, "请输入数量");
			} else {
				int i, dianji;
				i = m;
				dianji = m + 1;
				sum = goodsShop.getSalePrice()
						* Integer.parseInt(jt1.getText());
				for (int b = i; b < dianji; b++) {
					obj[b][0] = string;
					obj[b][1] = goodsShop.getTypeID();
					obj[b][2] = jt1.getText();
					obj[b][3] = goodsShop.getSalePrice();
					obj[b][4] = sum;
					m = dianji;
					shuliang = shuliang + Integer.parseInt(jt1.getText());
					zonger = sum + zonger;
					jLabel8.setText("商品总数：" + shuliang);
					jLabel9.setText("商品总额：  ￥" + zonger + "元");
				}
				jTable2 = new JTable(obj, str1);
				JScrollPane jsp = new JScrollPane(jTable2);
				// jPanel.add(jTable2.getTableHeader()).setBounds(0, 95, 1200,
				// 25);
				jPanel.add(jsp);
				jsp.setBounds(0, 105, 1500, 450);
				JOptionPane.showMessageDialog(OverNike.this, "添加成功");
			}
		}
		if (e.getSource() == jb3) {
			for (int n = 0; n < 50; n++) {
				for (int m = 0; m < 5; m++) {
					obj[n][m] = null;
				}
			}
			jLabel8.setText("商品总数：" + 0);
			jLabel9.setText("商品总额：  ￥" + 0 + "元");
			jTable3 = new JTable(obj, str1);
			JScrollPane jsp = new JScrollPane(jTable3);
			jPanel.add(jsp);
			jsp.setBounds(0, 105, 1500, 450);
			m = 0;
			zonger = 0;
			shuliang = 0;
			xxx = 0;
		}
	}

	public void cash(double qian) {// 现金支付
		this.setTitle("现金支付界面");
		this.setVisible(true);
		this.setSize(600, 400);
		this.setLocationRelativeTo(null);

		jPanel2 = new JPanel(null);
		setContentPane(jPanel2);
		jLabela = new JLabel("现金支付：");
		jta = new JTextField();
		jPanel2.add(jLabela);
		jPanel2.add(jta);
		jLabela.setBounds(100, 70, 150, 30);
		jLabela.setFont(new Font(null, 0, 24));
		jta.setBounds(240, 70, 150, 30);
		jta.setFont(new Font(null, 0, 24));
		jLabelb = new JLabel("总 金 额：   "
				+ new BigDecimal(qian).setScale(2, BigDecimal.ROUND_HALF_UP)
				+ "元");
		xxx = qian;
		jLabelc = new JLabel("找零："
				+ new BigDecimal(o).setScale(2, BigDecimal.ROUND_HALF_UP) + "元");
		jPanel2.add(jLabelb);
		jPanel2.add(jLabelc);
		jLabelb.setBounds(100, 140, 250, 30);
		jLabelb.setFont(new Font(null, 0, 24));
		jLabelc.setBounds(100, 210, 200, 30);
		jLabelc.setFont(new Font(null, 0, 24));
		JButton jaaButton = new JButton("结算");
		jPanel2.add(jaaButton);
		jaaButton.setBounds(220, 290, 100, 30);
		jaaButton.setFont(new Font(null, 0, 24));
		jaaButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				o = Integer.parseInt(jta.getText()) - xxx;
				jLabelc.setText("找零："
						+ new BigDecimal(o).setScale(2,
								BigDecimal.ROUND_HALF_UP) + "元");
			}
		});
	}

	public void Sur(double eney, double xie, String zuo, double ss, int x,
			Object[][] objs) {// 会员余额支付
		this.setTitle("会员余额支付界面");
		this.setVisible(true);
		this.setSize(600, 400);
		this.setLocationRelativeTo(null);

		jPanel3 = new JPanel(null);
		setContentPane(jPanel3);
		jLabeld = new JLabel("余额有："
				+ new BigDecimal(xie).setScale(2, BigDecimal.ROUND_HALF_UP));
		jPanel3.add(jLabeld);
		jLabeld.setBounds(150, 50, 250, 30);
		jLabeld.setFont(new Font(null, 0, 24));
		qqq = xie - eney;
		jLabele = new JLabel("总金额："
				+ new BigDecimal(eney).setScale(2, BigDecimal.ROUND_HALF_UP));
		jPanel3.add(jLabele);
		jLabele.setBounds(150, 100, 200, 30);
		jLabele.setFont(new Font(null, 0, 24));
		jLabelf = new JLabel("支付后余额:"
				+ new BigDecimal(qqq).setScale(2, BigDecimal.ROUND_HALF_UP));
		jLabelh = new JLabel("会员积分:"
				+ new BigDecimal(ss).setScale(2, BigDecimal.ROUND_HALF_UP));
		jPanel3.add(jLabelh);
		jPanel3.add(jLabelf);
		jLabelh.setBounds(150, 150, 210, 30);
		jLabelh.setFont(new Font(null, 0, 24));
		jLabelf.setBounds(150, 200, 250, 30);
		jLabelf.setFont(new Font(null, 0, 24));
		JButton jButton = new JButton("立即支付");
		jPanel3.add(jButton);
		jLabelf = new JLabel("请输入密码：");
		jPanel3.add(jLabelf);
		jLabelf.setBounds(150, 250, 250, 30);
		jLabelf.setFont(new Font(null, 0, 24));
		jps = new JPasswordField();
		jPanel3.add(jps);
		jps.setBounds(400, 250, 120, 30);
		jps.setFont(new Font(null, 0, 24));
		jButton.setBounds(300, 300, 140, 30);
		jButton.setFont(new Font(null, 0, 24));
		zonger = eney;
		ddd = zuo;
		www = ss;
		hhh = x;
		this.obj = objs;
		sql3 = "update goods set StockNum=StockNum-? where GoodsID =?";
		jButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				con = DbManager.getconn();
				sql = "update vip set balance=balance-?,integration=integration+? where telephone=? and pwd=?";
				sql2 = "select * from vip  where telephone=?";
				try {
					if (vipdao.selectvip(ddd, jps.getText()) == true) {
						pre = con.prepareStatement(sql);
						pre.setDouble(1, zonger);
						pre.setDouble(2, zonger);
						pre.setString(3, ddd);
						pre.setString(4, jps.getText());
						pre.execute();
						pre1 = con.prepareStatement(sql2);
						pre1.setString(1, ddd);
						res = pre1.executeQuery();
						while (res.next()) {
							vips.setIntegration(res.getDouble("integration"));
						}
						for (int m = 0; m < hhh; m++) {
							try {
								con = DbManager.getconn();
								pre2 = con.prepareStatement(sql3);
								pre2.setInt(1,
										Integer.parseInt(obj[m][2].toString()));
								pre2.setInt(2,
										Integer.parseInt(obj[m][0].toString()));
								pre2.execute();
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}

						}
						JOptionPane.showMessageDialog(OverNike.this, "会员支付成功！");
						jLabelf.setText("会员余额:"
								+ new BigDecimal(qqq).setScale(2,
										BigDecimal.ROUND_HALF_UP) + "元");
						jLabelh.setText("会员积分"
								+ new BigDecimal(vips.getIntegration())
										.setScale(2, BigDecimal.ROUND_HALF_UP));
					} else {
						JOptionPane.showMessageDialog(OverNike.this, "会员支付失败！");
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub

	}

	private void setTimer(JLabel time) {
		final JLabel varTime = time;
		Timer timeAction = new Timer(100, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				long timemillis = System.currentTimeMillis();
				// 转换日期显示格式
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				varTime.setText(df.format(new Date(timemillis)));
			}
		});
		timeAction.start();
	}
}
